<template>
  <div class="container">
    <div class="data-box" style="height:540px;">
      <!-- 搜索框 -->
      <MySearch @parent-searchBtn="searchBtn"></MySearch>

      <div class="add" style="width:100px;height:40px; float: right; margin-bottom: -10px;">
        <el-button size="mini" type="primary" @click="handleAdd()" style="width: 80px">
          新增
        </el-button>
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="宠物粮 ID">
                <span>{{ props.row.foodId }}</span>
              </el-form-item>
              <el-form-item label="宠物类型">
                <span>{{ props.row.petType }}</span>
              </el-form-item>
              <el-form-item label="宠物粮名称">
                <span>{{ props.row.foodName }}</span>
              </el-form-item>
              <el-form-item label="生产日期">
                <span>{{ props.row.proDate | dateFormat }}</span>
              </el-form-item>
              <el-form-item label="保质期">
                <span>{{ props.row.expDate | dateFormat }}</span>
              </el-form-item>
              <el-form-item label="数量">
                <span>{{ props.row.count }}</span>
              </el-form-item>
              <el-form-item label="价格">
                <span>{{ props.row.price }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="宠物粮 ID" prop="foodId">
        </el-table-column>
        <el-table-column label="宠物粮名称" prop="foodName">
        </el-table-column>
        <el-table-column label="生产日期" prop="proDate">
          <template slot-scope="scope">
            {{ scope.row.proDate | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页按钮 -->
    <div class="pagination-box">
      <my-pagination
        style="text-align: right; margin-right: 100px"
        @parent-getData="getData"
        :dataCount="dataCount"
      ></my-pagination>
    </div>

    <!-- 编辑弹出框 -->
    <div class="container" style="display: inline-block">
      <!-- Form -->
      <el-dialog :title="editBoxTitle" :visible.sync="dialogFormVisible">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="宠物类型">
            <el-input v-model="formInline.petType"></el-input>
          </el-form-item>
          <el-form-item label="粮食名称">
            <el-input v-model="formInline.foodName"></el-input>
          </el-form-item>
          <el-form-item label="生产日期">
            <div class="block">
              <el-date-picker
                v-model="formInline.proDate"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </div>
          </el-form-item>
          <el-form-item label="有效期至">
            <div class="block">
              <el-date-picker
                v-model="formInline.expDate"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </div>
          </el-form-item>
          <el-form-item label="库存量" label-width="68px">
            <el-input v-model="formInline.count"></el-input>
          </el-form-item>
          <el-form-item label="单价" label-width="68px">
            <el-input v-model="formInline.price"></el-input>
          </el-form-item> 
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleCancelBtn()">取 消</el-button>
          <el-button type="primary" @click="handleConfirmBtn()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 编辑弹出框多的end -->


  </div>
</template>
<script>
export default {
  data() {
    return {
      pageNum: 1,
      dataCount: 0,  // 获取数据总条数
      tableData: [{
        foodId: '1',
        petType: '狗',
        foodName: '狗粮',
        proDate: '2021-1-2',
        expDate: '2022-1-2',
        count: '5',
        price: '1000'
      }],
      dialogFormVisible: false, //编辑框弹出标志
      formInline: {   // 编辑框数据绑定
        foodId: '',
        petType: '',
        foodName: '',
        proDate: '',
        expDate: '',
        count: '',
        price: ''
      },
      editBoxTitle: '修改粮食信息'  // 编辑框的标题
    }
  },
  mounted() {
    this.getData(this.pageNum, 5);
    this.getDataCount();
  },
  methods: {
    handleAdd() {
      this.editBoxTitle = '添加粮食信息'
      this.dialogFormVisible = true
      this.formInline = {}
    },
    handleEdit(data) {
      this.editBoxTitle = '修改粮食信息'
      this.dialogFormVisible = true;
      this.initFormInline(data);
    },
    handleDelete(index, itemData) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        // 执行删除
        this.onDelData(itemData.foodId);
      }).then(() => {
        this.tableData.splice(index, 1)
        this.getData(this.pageNum, 5)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    handleCancelBtn(){
      this.dialogFormVisible = false
      this.formInline = {}
    },
    handleConfirmBtn(){
      if(this.editBoxTitle == '修改粮食信息'){
        this.onUpdate()
      } else {
        this.onAddData()
      }
    },
    // 根据页码发起请求获取数据
    getData(pageNum, pageSize) {
      // 分页组件调用此方法时，更新当前页码
      this.pageNum = pageNum;
      this.$axios
        .get("petFood/getByPage?pageNum=" + pageNum + "&pageSize=" + pageSize)
        .then((result) => {
          this.tableData = result.data;
        })
        .catch((err) => {
          alert(err);
        });
    },
    // 获取数据的总条数
    getDataCount() {
      this.$axios
        .get("petFood/getDataCount")
        .then((result) => {
          this.dataCount = result.data;
        })
        .catch((err) => {
          alert(err);
        });
    },
    onAddData(){
      this.$axios.post("petFood/insertPetFood", {
        petType: this.formInline.petType,
        foodName: this.formInline.foodName,
        proDate: this.formInline.proDate,
        expDate: this.formInline.expDate,
        count: this.formInline.count,
        price: this.formInline.price
      }).then((result) => {
        if (result.data == 1) {
            this.dialogFormVisible = false;
            // 添加数据后刷新数据
            this.getData(this.pageNum, 5);
            this.getDataCount()
            this.$message({
              showClose: true,
              message: "添加成功！",
              type: "success",
            });
        }
        this.formInline = {}
      }).catch((err) => {
        alert(err)
      });
    },
    onDelData(foodId){
      this.$axios.get("petFood/deletePetFoodById?foodId=" + foodId)
      .then((result) => {
        if(result.data == 1){
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        } else {
          this.$message({
            showClose: true,
            message: '删除失败',
            type: 'warning'
          });
        }
      }).catch((err) => {
        this.$message({
            showClose: true,
            message: '错误信息：' + err,
            type: 'warning'
        });
      });
    },
    onUpdate(){
      this.$axios
        .post("petFood/updatePetFoodById", {
          foodId: this.formInline.foodId,
          petType: this.formInline.petType,
          foodName: this.formInline.foodName,
          proDate: this.formInline.proDate,
          expDate: this.formInline.expDate,
          count: this.formInline.count,
          price: this.formInline.price
        })
        .then((result) => {
          if (result.data == 1) {
            this.dialogFormVisible = false;
            // 编辑数据后刷新数据
            this.getData(this.pageNum, 5);
          }
        })
        .then(() => {
          this.$message({
            showClose: true,
            message: "更新成功！",
            type: "success",
          });
        })
        .catch((err) => {
          alert(err);
        });
    },
    initFormInline(data){
      let newForm = {
        foodId: data.foodId,
        petType: data.petType,
        foodName: data.foodName,
        proDate: data.proDate,
        expDate: data.expDate,
        count: data.count,
        price: data.price
      };
      this.formInline = newForm;
    },
    searchBtn(value){
      if(value != ''){
        let newTable = this.tableData.filter(
          item => item.foodName.toLowerCase().includes(value.toLowerCase())
        )
        this.tableData = newTable;
      } else {
        this.getData(this.pageNum, 5)
      }
    }
  },
}
</script>

<style>
.demo-form-inline {
  padding: 0px 20px;
}

.el-form-item .el-input {
  width: 250px;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 30%;
}
</style>
